package com.ruoyi.material.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.domain.BaseLabelType;
import com.ruoyi.material.domain.BaseMaterial;
import com.ruoyi.material.domain.vo.BaseLabelTypeTreeVo;
import com.ruoyi.material.domain.vo.BaseMaterialTreeVo;
import com.ruoyi.material.mapper.BaseLabelTypeMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.BaseLabelMapper;
import com.ruoyi.material.domain.BaseLabel;
import com.ruoyi.material.service.IBaseLabelService;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * 标签类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-23
 */
@Service
public class BaseLabelServiceImpl implements IBaseLabelService 
{
    @Autowired(required = false)
    private BaseLabelMapper baseLabelMapper;
    @Autowired(required = false)
    private BaseLabelTypeMapper baseLabelTypeMapper;
    /**
     * 查询标签类型
     * 
     * @param id 标签类型ID
     * @return 标签类型
     */
    @Override
    public BaseLabel selectBaseLabelById(Long id)
    {
        return baseLabelMapper.selectBaseLabelById(id);
    }

    /**
     * 查询标签类型列表
     * 
     * @param baseLabel 标签类型
     * @return 标签类型
     */
    @Override
    public List<BaseLabel> selectBaseLabelList(BaseLabel baseLabel)
    {
        return baseLabelMapper.selectBaseLabelList(baseLabel);
    }

    /**
     * 新增标签类型
     * 
     * @param baseLabel 标签类型
     * @return 结果
     */
    @Override
    public int insertBaseLabel(BaseLabel baseLabel)
    {
        baseLabel.setCreateTime(DateUtils.getNowDate());
        return baseLabelMapper.insertBaseLabel(baseLabel);
    }

    /**
     * 修改标签类型
     * 
     * @param baseLabel 标签类型
     * @return 结果
     */
    @Override
    public int updateBaseLabel(BaseLabel baseLabel)
    {
        baseLabel.setUpdateTime(DateUtils.getNowDate());
        return baseLabelMapper.updateBaseLabel(baseLabel);
    }

    /**
     * 批量删除标签类型
     * 
     * @param ids 需要删除的标签类型ID
     * @param updateBy
     * @return 结果
     */
    @Override
    public int deleteBaseLabelByIds(@Param("ids") Long[] ids,@Param("updateBy") String updateBy)
    {
        return baseLabelMapper.deleteBaseLabelByIds(ids,updateBy);
    }

    /**
     * 删除标签类型信息
     * 
     * @param id 标签类型ID
     * @return 结果
     */
    @Override
    public int deleteBaseLabelById(Long id)
    {
        return baseLabelMapper.deleteBaseLabelById(id);
    }

    /**
     * 通过标签名称获取标签
     * @param label 标签名称
     * @return
     */
    public BaseLabel selectBaseLabelByLabel(String label){
        return  baseLabelMapper.selectBaseLabelByLabel(label);
    }


    /**
     * 批量插入
     */
    public int insertBaseLabelList(List<BaseLabel> list){
        return baseLabelMapper.insertBaseLabelList(list);
    }
    /**
     * 获取已有标签id和标签名称的map
     *
     * @return
     */
//    public Map<Integer,String> selectBaseLabelMap();
    @Override
    public List selectBaseLabelMap(){
        return baseLabelMapper.selectBaseLabelMap();
    }
    /**
     *  通过文件ID查询对应的标签
     */
    @Override
    public List<String> selectBaseLabelByFileCode(@Param("file_code")Integer fileCode){
        return baseLabelMapper.selectBaseLabelByFileCode(fileCode);
    }
    /**
     *  通过文件ID查询对应的标签
     */
    @Override
    public List<BaseLabel> selectBLByFileCode(@Param("file_code")Integer fileCode){
        return baseLabelMapper.selectBLByFileCode(fileCode);
    }
    /**
     * 标签
     * 构建前端所需要下拉树结构
     * @return 下拉树结构列表
     */
    @Override
    public List<BaseLabelTypeTreeVo> buildLabelTreeSelect()
    {
        List<BaseLabelTypeTreeVo> blttvList=new ArrayList<>();//第一级List
        List<BaseLabelType> baseLabelTypeList=new ArrayList<>();
        BaseLabelType baseLabelType=new BaseLabelType();
        baseLabelTypeList = baseLabelTypeMapper.selectAllType();

        for (BaseLabelType blt:baseLabelTypeList) {
            BaseLabelTypeTreeVo blttv=new BaseLabelTypeTreeVo();//第一级
            blttv.setId(blt.getId().intValue());
            blttv.setLabel(blt.getLabelType());

            BaseLabel baseLabel=new BaseLabel();
            baseLabel.setTypeCode(blt.getId());
            List<BaseLabel> baseLabelList= baseLabelMapper.selectBaseLabelList(baseLabel);
            List<BaseLabelTypeTreeVo> blttvChildren=new ArrayList<>();//第一级children，即第二级List
            for (BaseLabel bl: baseLabelList) {
                BaseLabelTypeTreeVo blttvChild=new BaseLabelTypeTreeVo();//第二级
                blttvChild.setId(bl.getId().intValue());
                blttvChild.setLabel(bl.getLabel());//第二级无children
                blttvChild.setChildren(null);
                blttvChildren.add(blttvChild);
            }
            blttv.setChildren(blttvChildren);
            blttvList.add(blttv);
        }
        return blttvList;
    }


    /**
     * 根据文件id找到标签
     * @return
     */
    @Override
    public String[] showFileLabels(@Param("id")Integer id){
        List<BaseLabel> baseLabelList=new ArrayList<>();
        baseLabelList=selectBLByFileCode(id);
        String str="";
        int count=0;
        for (BaseLabel b:baseLabelList) {
            count++;
            str+=b.getTypeCode()+","+b.getId();
            if(count!=baseLabelList.size()){
                str+=" ";
            }
        }
        System.out.println(str);
        String[] strings=str.split(" ");
        return strings;
    }
}
